from django.utils.decorators import method_decorator

from gis.common.django_ext.decorators import validate_parameters
from gis.common.django_ext.views import BaseView, PaginationResponse
from itpay.core.agency import blacklist_service
from itpay.server_agency.decorators import require_login
from itpay.core.agency.const import AgencyType
from . import blacklist_form
from .helper import wrapper_name


class BlackListView(BaseView):
    @method_decorator(require_login())
    @method_decorator(validate_parameters(blacklist_form.GetBlackListSchema))
    def get(self, request, cleaned_data):
        user = request.user
        if AgencyType(user['agency_type']) == AgencyType.ADMIN_AGENCY:
            cleaned_data['parent_id'] = user['id']
        elif AgencyType(user['agency_type']) == AgencyType.NORMAL_AGENCY:
            cleaned_data['agency_id'] = user['id']
        total, blacklist = blacklist_service.list_blacklist(**cleaned_data)
        return PaginationResponse(total, [wrapper_name(e) for e in blacklist])

    @method_decorator(require_login())
    @method_decorator(validate_parameters(blacklist_form.AddBlackListSchema))
    def post(self, request, cleaned_data):
        agency_id = request.user_id
        return blacklist_service.add_blacklist_user(agency_id=agency_id, **cleaned_data)


class BlackDetailView(BaseView):
    @method_decorator(require_login())
    def delete(self, request, blacklist_id):
        blacklist_service.delete_blacklist_user(blacklist_id)
